import os

import requests
import time
import base64
from 运维工具.登录逻辑.utils.base64_to_image import decode_base64_to_image
def getCheckCode(**kwargs):


    cookies = {
        'JSESSIONID': kwargs.get('JSESSIONID'),
        'tempTokenKey': kwargs.get('tempTokenKey'),
    }

    headers = {
        'Host': 'gzmh.zwfw.guizhou.gov.cn',
        'sec-ch-ua-platform': '"Windows"',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0',
        'sec-ch-ua': '"Microsoft Edge";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
        'sec-ch-ua-mobile': '?0',
        'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Dest': 'image',
        'Referer': 'https://gzmh.zwfw.guizhou.gov.cn/ucenter/simpleLogin?=&response_type=code&client_id=dgov-portal&redirect_uri=https://gzmh.zwfw.guizhou.gov.cn/login/callback.html&version=guizhou&isHLW=true',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    }

    params = {
        'csrf': kwargs.get('csrf'),
        'v': int(time.time() * 1000),
    }

    response = requests.get('https://gzmh.zwfw.guizhou.gov.cn/ucenter/web/checkcode', params=params, cookies=cookies,
                            headers=headers)
    base64_encoded = base64.b64encode(response.content)
    # 把字节类型的 Base64 编码转换为字符串
    base64_string = base64_encoded.decode('ascii')
    # 调用base转图片
    # decode_base64_to_image(base64_string, '../static/img/登录验证码.png')
    # 获取当前文件的目录
    current_dir = os.path.dirname(os.path.abspath(__file__))


    # 获取父目录
    parent_dir = os.path.dirname(current_dir) ##D:\pyWorkSpace\tkinter\运维工具

    # 构建到static/img的路径
    img_dir = os.path.join(parent_dir,'static', 'img')
    decode_base64_to_image(base64_string, f'{img_dir}\\登录验证码.png')


